package com.winner.crmsoftware.employee;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import com.winner.crmsoftware.addcustomer.AddCustomerActivity;
import com.winner.crmsoftware.customers.ListCustomerActivity;
import com.winner.crmsoftware.location.GPSTracker;
import com.winner.crmsoftware.main.MainProgramActivity;
import com.winner.crmsoftware.main.R;
import com.winner.crmsoftware.ultil.PreferentUltils;
import com.winner.srmsoftware.json.JSONParser;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class EmployeeInfomationActivity extends Activity{

	EditText edt_id;
	EditText edt_name;
	TextView tv_latitude;
	TextView tv_longitude;
	Button btn_submit;
	Button btn_update;
	
	double lati;
	double longti;
	
	
	JSONParser jsonParser = new JSONParser();
	public static final String URL_UPDATE_LOCATION = "http://103.28.37.16:8080/crm_software/add_new_employee.php";

	private ProgressDialog pDialog;
	GPSTracker gpsTracker;
	
	 SharedPreferences pref;
	 Editor editor;

	private static final String TAG_SUCCESS = "success";

	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.employee_infomation);
		
		 pref = getApplicationContext().getSharedPreferences(PreferentUltils.EMPLOYEE_INFO, MODE_PRIVATE); 
		 editor = pref.edit();
		 
		
		edt_id = (EditText) findViewById(R.id.edt_id_employee);
		edt_name = (EditText) findViewById(R.id.edt_name_employee);
		tv_latitude = (TextView)findViewById(R.id.tv_latitude_employee);
		tv_longitude = (TextView) findViewById(R.id.tv_longitude_employee);
		btn_submit = (Button)findViewById(R.id.btn_add_employee);
		btn_update = (Button) findViewById(R.id.btn_update_employee);
		
		btn_submit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				new AddNewEmployee().execute();
			}
		});
		
		btn_update.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				
			}
		});
		
		gpsTracker = new GPSTracker(this);
		if (!gpsTracker.canGetLocation()) {
			gpsTracker.showSettingsAlert();
		}

		new GetLocation().execute();
		
		
	}
	
	private class GetLocation extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			// Showing progress dialog
			pDialog = new ProgressDialog(EmployeeInfomationActivity.this);
			pDialog.setMessage("Please wait...");
			pDialog.setCancelable(false);
			pDialog.show();

		}

		@SuppressLint("DefaultLocale")
		@Override
		protected Void doInBackground(Void... arg0) {
			// Creating service handler class instance
			lati = gpsTracker.getLatitude();
			longti = gpsTracker.getLongitude();

			// Making a request to url and getting response
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);
			// Dismiss the progress dialog
			if (pDialog.isShowing())
				pDialog.dismiss();
			/**
			 * Updating parsed JSON data into ListView
			 * */
			tv_latitude.setText(String.valueOf(lati));
			tv_longitude.setText(String.valueOf(longti));
		}
	}
	
	class AddNewEmployee extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(EmployeeInfomationActivity.this);
			pDialog.setMessage("Creating Product..");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Creating product
		 * */
		protected String doInBackground(String... args) {
			
			String employee_id = edt_id.getText().toString();
			String name = edt_name.getText().toString();
			String latitude = tv_latitude.getText().toString();
			String longitude = tv_longitude.getText().toString();
			
			editor.putString(PreferentUltils.ID_EMPLOYEE,employee_id);
			editor.putString(PreferentUltils.NAME_STRING,name);
			editor.commit();

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			
			params.add(new BasicNameValuePair("employee_id", employee_id));
			params.add(new BasicNameValuePair("name", name));
			params.add(new BasicNameValuePair("latitude", latitude));
			params.add(new BasicNameValuePair("longitude", longitude));

			// getting JSON Object
			// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(
					URL_UPDATE_LOCATION, "POST", params);

			try {
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// successfully created product
					Intent i = new Intent(getApplicationContext(),
							MainProgramActivity.class);
					startActivity(i);
					finish();
				} else {
					// failed to create product
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	
	
}
